# Replication Code for Appendix Tables A3 through A12
# James M. Curry and Leah Rosenstiel
# Last Updated 12/3/2025

rm(list=ls())
library(tidyverse)
source("functions.R")


#### Pre-trends (Table A3) ----

# Read in the data
grants <- read_csv("grants.csv")

###### All legislation
# Create new treated and control variables
committee_grants <- grants %>%
  filter(!is.na(on_commit) & !is.na(on_commit_prior) & !is.na(grant)) %>%
  mutate(treated = on_commit,
         control = 1 - on_commit,
         state_year_commit = paste0(state_abbr,year,committee_code),
         state_program = paste0(state_abbr, program),
         log_grant = log(grant+1)) 

## Create lags and leads and calculate weights  
## Note: compare_var is also different from above analysis
commit_analysis_data <- make_boot_data(treat_var = "treated",
                                       untreat_var = "control",
                                       time_var = "year", 
                                       lag_control = "control",
                                       compare_var = "state_year_commit", 
                                       unit_var= "state_program", d_var="log_grant", 
                                       lead_yrs = 2,lag_yrs = 2, df = committee_grants)

# Calculate change in grant amount (long difference)
commit_analysis_data <- commit_analysis_data %>%
  mutate(grant_change = log_grant_p2 - log_grant_p1)


# Bootstrap
commit_coefs <- matrix(NA, nrow=1000, ncol=1)
commit_analysis_data <- commit_analysis_data[order(commit_analysis_data$state_program, 
                                                   commit_analysis_data$year), ]
all_state_programs <- unique(commit_analysis_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- commit_analysis_data[commit_analysis_data$state_program%in%boot_state_program,]
  commit_coefs[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}

commit_analysis_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(commit_coefs, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(commit_coefs,2, quantile, 0.975),
            ci025=apply(commit_coefs,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),df=nrow(commit_analysis_data[commit_analysis_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(commit_analysis_data[commit_analysis_data$obs_weight!=0,])

##### Unorthodox Only
committee_grants$unorthodox <- ifelse(committee_grants$s_nocommreport +
                                        committee_grants$no_conference>0,1,0)
committee_grants$treated <- committee_grants$on_commit * 
                                     committee_grants$unorthodox

## Create lags and leads and calculate weights  
## Note: compare_var is also different from above analysis
unorth_commit_analysis_data <- make_boot_data(treat_var = "treated",
                                              untreat_var = "control",
                                              time_var = "year", 
                                              lag_control = "control",
                                              compare_var = "state_year_commit", 
                                              unit_var= "state_program", d_var="log_grant", 
                                              lead_yrs = 2,lag_yrs = 2, df = committee_grants)

# Calculate change in grant amount (long difference)
unorth_commit_analysis_data <- unorth_commit_analysis_data %>%
  mutate(grant_change = log_grant_p2 - log_grant_p1)


# Bootstrap
commit_coefs2 <- matrix(NA, nrow=1000, ncol=1)
unorth_commit_analysis_data <- unorth_commit_analysis_data[order(unorth_commit_analysis_data$state_program, 
                                                                 unorth_commit_analysis_data$year), ]
all_state_programs <- unique(unorth_commit_analysis_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- unorth_commit_analysis_data[unorth_commit_analysis_data$state_program%in%boot_state_program,]
  commit_coefs2[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}

unorth_commit_analysis_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(commit_coefs2, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(commit_coefs2,2, quantile, 0.975),
            ci025=apply(commit_coefs2,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),df=nrow(unorth_commit_analysis_data[unorth_commit_analysis_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(unorth_commit_analysis_data[unorth_commit_analysis_data$obs_weight!=0,])


##### Regular Order Only
committee_grants$treated <- ifelse(committee_grants$on_commit==1 & 
                                     committee_grants$unorthodox==0,1,0)

## Create lags and leads and calculate weights  
## Note: compare_var is also different from above analysis
orth_commit_analysis_data <- make_boot_data(treat_var = "treated",
                                            untreat_var = "control",
                                            time_var = "year", 
                                            lag_control = "control",
                                            compare_var = "state_year_commit", 
                                            unit_var= "state_program", d_var="log_grant", 
                                            lead_yrs = 2,lag_yrs = 2, 
                                            df = committee_grants)

# Calculate change in grant amount (long difference)
orth_commit_analysis_data <- orth_commit_analysis_data %>%
  mutate(grant_change = log_grant_p2 - log_grant_p1)


# Bootstrap
commit_coefs3 <- matrix(NA, nrow=1000, ncol=1)
orth_commit_analysis_data <- orth_commit_analysis_data[order(orth_commit_analysis_data$state_program, 
                                                             orth_commit_analysis_data$year), ]
all_state_programs <- unique(orth_commit_analysis_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- orth_commit_analysis_data[orth_commit_analysis_data$state_program%in%boot_state_program,]
  commit_coefs3[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}

orth_commit_analysis_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(commit_coefs3, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(commit_coefs3, 2, quantile, 0.975),
            ci025=apply(commit_coefs3, 2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),df=nrow(orth_commit_analysis_data[orth_commit_analysis_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(orth_commit_analysis_data[orth_commit_analysis_data$obs_weight!=0,])


#### Unorthodox Lawmaking Over Time (Table A4) ----
# Estimate each congress separately
# 114, 109, 105, 99, and 98

# Read in grants data
grants <- read_csv("grants.csv")
# Create variables needed for analysis
grants <- grants %>%
  mutate(lag_control = 1 - on_commit, #units are only included if they switch on to committee
         state_year_cong = paste(state_abbr,year,congress), #compare same state, same year, same reauth congress
         state_program = paste(state_abbr, program),
         year_cong = paste(year, congress),
         log_grant = log(grant+1),
         prior_year = year - 1) %>%
  left_join(select(grants,program,state,prior_year=year,prior_grant=grant)) %>%
  unique

# Only look at 114th Congress
grants <- grants %>%
  mutate(unorthodox = ifelse(s_nocommreport + no_conference > 0, 1, 0),
         congress114 = ifelse(congress==114,1,0),
         treated = on_commit * unorthodox * congress114, #treated units are unorthdox law and on committee
         control = on_commit * (1 - unorthodox) * congress114) #control units are regular law and on committee 


## Create lags and leads and calculate weights  
unorthodox_data114 <- make_boot_data(treat_var = "treated",untreat_var = "control",
                                     lag_control = "lag_control", time_var = "year", 
                                     compare_var = "year_cong", 
                                     unit_var= "state_program", d_var="log_grant", 
                                     lead_yrs = 2,lag_yrs = 2, df = grants)

# Calculate change in grant amount (long difference)
unorthodox_data114 <- unorthodox_data114 %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)

# Bootstrap
unorthodox114_coefs <- matrix(NA, nrow=1000, ncol=3)
unorthodox_data114 <- unorthodox_data114[order(unorthodox_data114$state_program, 
                                               unorthodox_data114$year), ]
all_state_programs <- unique(unorthodox_data114$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- unorthodox_data114[unorthodox_data114$state_program%in%boot_state_program,]
  unorthodox114_coefs[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  unorthodox114_coefs[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  unorthodox114_coefs[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}

unorthodox_data114 %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(unorthodox114_coefs, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(unorthodox114_coefs,2, quantile, 0.975),
            ci025=apply(unorthodox114_coefs,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),
                     df=nrow(unorthodox_data114[unorthodox_data114$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(unorthodox_data114[unorthodox_data114$obs_weight!=0,])


### Only look at 98th Congress
grants <- grants %>%
  mutate(unorthodox = ifelse(s_nocommreport + no_conference > 0, 1, 0),
         congress98 = ifelse(congress==98,1,0),
         treated = on_commit * unorthodox * congress98, #treated units are unorthdox law and on committee
         control = on_commit * (1 - unorthodox) * congress98) #control units are regular law and on committee 


## Create lags and leads and calculate weights  
unorthodox_data98 <- make_boot_data(treat_var = "treated",untreat_var = "control",
                                    lag_control = "lag_control", time_var = "year", 
                                    compare_var = "year_cong", 
                                    unit_var= "state_program", d_var="log_grant", 
                                    lead_yrs = 2,lag_yrs = 2, df = grants)

# Calculate change in grant amount (long difference)
unorthodox_data98 <- unorthodox_data98 %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)

# Bootstrap
unorthodox98_coefs <- matrix(NA, nrow=1000, ncol=3)
unorthodox_data98 <- unorthodox_data98[order(unorthodox_data98$state_program, 
                                             unorthodox_data98$year), ]
all_state_programs <- unique(unorthodox_data98$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- unorthodox_data98[unorthodox_data98$state_program%in%boot_state_program,]
  # make sure there is at least one treated unit
  if (nrow(data_sub[data_sub$obs_weight>0,])>=1) {
    unorthodox98_coefs[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
      sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
    unorthodox98_coefs[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
      sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
    unorthodox98_coefs[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
      sum(data_sub$treated * data_sub$obs_weight, na.rm=T)}
}

# remove any NAs
unorthodox98_coefs <- na.omit(unorthodox98_coefs)

unorthodox_data98 %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(unorthodox98_coefs, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(unorthodox98_coefs,2, quantile, 0.975),
            ci025=apply(unorthodox98_coefs,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),
                     df=nrow(unorthodox_data98[unorthodox_data98$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(unorthodox_data98[unorthodox_data98$obs_weight!=0,])


### 99th Congress
grants <- grants %>%
  mutate(unorthodox = ifelse(s_nocommreport + no_conference > 0, 1, 0),
         congress99 = ifelse(congress==99,1,0),
         treated = on_commit * unorthodox * congress99, #treated units are unorthdox law and on committee
         control = on_commit * (1 - unorthodox) * congress99) #control units are regular law and on committee 


## Create lags and leads and calculate weights  
unorthodox_data99 <- make_boot_data(treat_var = "treated",untreat_var = "control",
                                    lag_control = "lag_control", time_var = "year", 
                                    compare_var = "year_cong", 
                                    unit_var= "state_program", d_var="log_grant", 
                                    lead_yrs = 2,lag_yrs = 2, df = grants)

# Calculate change in grant amount (long difference)
unorthodox_data99 <- unorthodox_data99 %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)

# Bootstrap
unorthodox99_coefs <- matrix(NA, nrow=1000, ncol=3)
unorthodox_data99 <- unorthodox_data99[order(unorthodox_data99$state_program, 
                                             unorthodox_data99$year), ]
all_state_programs <- unique(unorthodox_data99$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- unorthodox_data99[unorthodox_data99$state_program%in%boot_state_program,]
  # make sure there is at least one treated unit
  if (nrow(data_sub[data_sub$obs_weight>0,])>=1) {
    unorthodox99_coefs[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
      sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
    unorthodox99_coefs[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
      sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
    unorthodox99_coefs[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
      sum(data_sub$treated * data_sub$obs_weight, na.rm=T)}
}

# remove any NAs
unorthodox99_coefs <- na.omit(unorthodox99_coefs)

unorthodox_data99 %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(unorthodox99_coefs, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(unorthodox99_coefs,2, quantile, 0.975),
            ci025=apply(unorthodox99_coefs,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),
                     df=nrow(unorthodox_data99[unorthodox_data99$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(unorthodox_data99[unorthodox_data99$obs_weight!=0,])


## 105th Congress
grants <- grants %>%
  mutate(unorthodox = ifelse(s_nocommreport + no_conference > 0, 1, 0),
         congress105 = ifelse(congress==105,1,0),
         treated = on_commit * unorthodox * congress105, #treated units are unorthdox law and on committee
         control = on_commit * (1 - unorthodox) * congress105) #control units are regular law and on committee 


## Create lags and leads and calculate weights  
unorthodox_data105 <- make_boot_data(treat_var = "treated",untreat_var = "control",
                                     lag_control = "lag_control", time_var = "year", 
                                     compare_var = "year_cong", 
                                     unit_var= "state_program", d_var="log_grant", 
                                     lead_yrs = 2,lag_yrs = 2, df = grants)

# Calculate change in grant amount (long difference)
unorthodox_data105 <- unorthodox_data105 %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)

# Bootstrap
unorthodox105_coefs <- matrix(NA, nrow=1000, ncol=3)
unorthodox_data105 <- unorthodox_data105[order(unorthodox_data105$state_program, 
                                               unorthodox_data105$year), ]
all_state_programs <- unique(unorthodox_data105$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- unorthodox_data105[unorthodox_data105$state_program%in%boot_state_program,]
  # make sure there is at least one treated unit
  if (nrow(data_sub[data_sub$obs_weight>0,])>=1) {
    unorthodox105_coefs[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
      sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
    unorthodox105_coefs[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
      sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
    unorthodox105_coefs[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
      sum(data_sub$treated * data_sub$obs_weight, na.rm=T)}
}

# remove any NAs
unorthodox105_coefs <- na.omit(unorthodox105_coefs)

unorthodox_data105 %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(unorthodox105_coefs, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(unorthodox105_coefs,2, quantile, 0.975),
            ci025=apply(unorthodox105_coefs,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),
                     df=nrow(unorthodox_data105[unorthodox_data105$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(unorthodox_data105[unorthodox_data105$obs_weight!=0,])


## 109th Congress
grants <- grants %>%
  mutate(unorthodox = ifelse(s_nocommreport + no_conference > 0, 1, 0),
         congress109 = ifelse(congress==109,1,0),
         treated = on_commit * unorthodox * congress109, #treated units are unorthdox law and on committee
         control = on_commit * (1 - unorthodox) * congress109) #control units are regular law and on committee 


## Create lags and leads and calculate weights  
unorthodox_data109 <- make_boot_data(treat_var = "treated",untreat_var = "control",
                                     lag_control = "lag_control", time_var = "year", 
                                     compare_var = "year_cong", 
                                     unit_var= "state_program", d_var="log_grant", 
                                     lead_yrs = 2,lag_yrs = 2, df = grants)

# Calculate change in grant amount (long difference)
unorthodox_data109 <- unorthodox_data109 %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)

# Bootstrap
unorthodox109_coefs <- matrix(NA, nrow=1000, ncol=3)
unorthodox_data109 <- unorthodox_data109[order(unorthodox_data109$state_program, 
                                               unorthodox_data109$year), ]
all_state_programs <- unique(unorthodox_data109$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- unorthodox_data109[unorthodox_data109$state_program%in%boot_state_program,]
  # make sure there is at least one treated unit
  if (nrow(data_sub[data_sub$obs_weight>0,])>=1) {
    unorthodox109_coefs[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
      sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
    unorthodox109_coefs[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
      sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
    unorthodox109_coefs[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
      sum(data_sub$treated * data_sub$obs_weight, na.rm=T)}
}

# remove any NAs
unorthodox109_coefs <- na.omit(unorthodox109_coefs)

unorthodox_data109 %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(unorthodox109_coefs, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(unorthodox109_coefs,2, quantile, 0.975),
            ci025=apply(unorthodox109_coefs,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),
                     df=nrow(unorthodox_data109[unorthodox_data109$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(unorthodox_data109[unorthodox_data109$obs_weight!=0,])

#### Unorthodox Lawmaking Including Post Committee Adjustment (Table A5) ----
### Unorthodox Lawmaking
committee_grants <- grants %>%
  filter(!is.na(on_commit) & !is.na(on_commit_prior) & !is.na(grant)) %>%
  mutate(unorthodox = ifelse(s_nocommreport + no_conference + s_postcommadj > 0, 1, 0),
         treated = on_commit * unorthodox,
         control = 1 - on_commit,
         state_year = paste0(state_abbr,year),
         state_year_commit = paste0(state_abbr,year,committee_code),
         state_program = paste0(state_abbr, program),
         commit_year = paste(committee_code, year),
         program_year = paste(program, year),
         log_grant = log(grant+1),
         prior_year = year - 1) 

## Create lags and leads and calculate weights  
## Note: compare_var is also different from above analysis
unorth_commit_analysis_data <- make_boot_data(treat_var = "treated",
                                              untreat_var = "control",
                                              time_var = "year", 
                                              lag_control = "control",
                                              compare_var = "state_year_commit", 
                                              unit_var= "state_program", d_var="log_grant", 
                                              lead_yrs = 2,lag_yrs = 2, df = committee_grants)

# Calculate change in grant amount (long difference)
unorth_commit_analysis_data <- unorth_commit_analysis_data %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)


# Bootstrap
commit_coefs2 <- matrix(NA, nrow=1000, ncol=3)
unorth_commit_analysis_data <- unorth_commit_analysis_data[order(unorth_commit_analysis_data$state_program, 
                                                                 unorth_commit_analysis_data$year), ]
all_state_programs <- unique(unorth_commit_analysis_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- unorth_commit_analysis_data[unorth_commit_analysis_data$state_program%in%boot_state_program,]
  commit_coefs2[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  commit_coefs2[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  commit_coefs2[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}

unorth_commit_analysis_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(commit_coefs2, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(commit_coefs2,2, quantile, 0.975),
            ci025=apply(commit_coefs2,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),df=nrow(unorth_commit_analysis_data[unorth_commit_analysis_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(unorth_commit_analysis_data[unorth_commit_analysis_data$obs_weight!=0,])


## Regular Order (excludes post-committee adjustment)
committee_grants <- grants %>%
  filter(!is.na(on_commit) & !is.na(on_commit_prior) & !is.na(grant)) %>%
  mutate(unorthodox = ifelse(s_nocommreport + no_conference + s_postcommadj > 0, 1, 0),
         treated = ifelse(on_commit==1 & unorthodox==0, 1, 0),
         control = 1 - on_commit,
         state_year = paste0(state_abbr,year),
         state_year_commit = paste0(state_abbr,year,committee_code),
         state_program = paste0(state_abbr, program),
         commit_year = paste(committee_code, year),
         program_year = paste(program, year),
         log_grant = log(grant+1),
         prior_year = year - 1) 

## Create lags and leads and calculate weights  
## Note: compare_var is also different from above analysis
orth_commit_analysis_data <- make_boot_data(treat_var = "treated",
                                            untreat_var = "control",
                                            time_var = "year", 
                                            lag_control = "control",
                                            compare_var = "state_year_commit", 
                                            unit_var= "state_program", d_var="log_grant", 
                                            lead_yrs = 2,lag_yrs = 2, 
                                            df = committee_grants)

# Calculate change in grant amount (long difference)
orth_commit_analysis_data <- orth_commit_analysis_data %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)


# Bootstrap
commit_coefs3 <- matrix(NA, nrow=1000, ncol=3)
orth_commit_analysis_data <- orth_commit_analysis_data[order(orth_commit_analysis_data$state_program, 
                                                             orth_commit_analysis_data$year), ]
all_state_programs <- unique(orth_commit_analysis_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- orth_commit_analysis_data[orth_commit_analysis_data$state_program%in%boot_state_program,]
  commit_coefs3[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  commit_coefs3[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  commit_coefs3[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}

orth_commit_analysis_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(commit_coefs3, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(commit_coefs3, 2, quantile, 0.975),
            ci025=apply(commit_coefs3, 2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),df=nrow(orth_commit_analysis_data[orth_commit_analysis_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(orth_commit_analysis_data[orth_commit_analysis_data$obs_weight!=0,])






#### Unorthodox Lawmaking Including Post Committee Adjustment (Table A6) ----
## Post-Committee Adjustment
grants <- grants %>%
  mutate(unorthodox = ifelse(s_postcommadj == 1, 1, 0),
         treated = on_commit * unorthodox, #treated units are unorthdox law and on committee
         control = on_commit * (1 - unorthodox)) #control units are regular law and on committee 

## Create lags and leads and calculate weights  
report_data <- make_boot_data(treat_var = "treated",untreat_var = "control",
                              lag_control = "lag_control", time_var = "year", 
                              compare_var = "year_cong", 
                              unit_var= "state_program", d_var="log_grant", 
                              lead_yrs = 2,lag_yrs = 2, df = grants)

# Calculate change in grant amount (long difference)
report_data <- report_data %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)

# Bootstrap
report_coefs <- matrix(NA, nrow=1000, ncol=3)
report_data <- report_data[order(report_data$state_program, 
                                 report_data$year), ]
all_state_programs <- unique(report_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- report_data[report_data$state_program%in%boot_state_program,]
  report_coefs[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  report_coefs[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  report_coefs[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}

report_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(report_coefs, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(report_coefs,2, quantile, 0.975),
            ci025=apply(report_coefs,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),
                     df=nrow(report_data[report_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(report_data[report_data$obs_weight!=0,])

## Composite Unorthodox
# Treated Units = switched onto committee and unorthodox reauth
# Control Units = switched onto committee and regular reauth
# Unorthodox = didn't have committee report and/or conference and/or post-committee adjustment

grants <- grants %>%
  mutate(unorthodox = ifelse(s_nocommreport + no_conference + s_postcommadj > 0, 1, 0),
         treated = on_commit * unorthodox, #treated units are unorthdox law and on committee
         control = on_commit * (1 - unorthodox)) #control units are regular law and on committee 

## Create lags and leads and calculate weights  
unorthodox_data <- make_boot_data(treat_var = "treated",untreat_var = "control",
                                  lag_control = "lag_control", time_var = "year", 
                                  compare_var = "year_cong", 
                                  unit_var= "state_program", d_var="log_grant", 
                                  lead_yrs = 2,lag_yrs = 2, df = grants)

# Calculate change in grant amount (long difference)
unorthodox_data <- unorthodox_data %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)

# Bootstrap
unorthodox_coefs <- matrix(NA, nrow=1000, ncol=3)
unorthodox_data <- unorthodox_data[order(unorthodox_data$state_program, 
                                         unorthodox_data$year), ]
all_state_programs <- unique(unorthodox_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- unorthodox_data[unorthodox_data$state_program%in%boot_state_program,]
  unorthodox_coefs[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  unorthodox_coefs[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  unorthodox_coefs[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}

unorthodox_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(unorthodox_coefs, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(unorthodox_coefs,2, quantile, 0.975),
            ci025=apply(unorthodox_coefs,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),
                     df=nrow(unorthodox_data[unorthodox_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(unorthodox_data[unorthodox_data$obs_weight!=0,])


#### Chair Advantage (Table A7) ----
# Treated Units = became chair and reauthorized
# Control Units = became chair and have yet to reauthorize
# Match based on state & committee
# reason to match this way is then you only look at states represented by
# committee members to mirror the previous analysis


# Read in the data again
grants <- read_csv("grants.csv")

### All Legislation
# Create new treated and control variables
committee_grants <- grants %>%
  filter(!is.na(on_commit) & !is.na(on_commit_prior) & !is.na(grant)) %>%
  mutate(treated = chair,
         control = 1 - chair,
         lag_control = 1 - chair,
         state_year_commit = paste0(state_abbr,year,committee_code),
         state_program = paste0(state_abbr, program),
         log_grant = log(grant+1)) 
# Create lags and leads and calculate weights  
commit_analysis_data <- make_boot_data(treat_var = "treated",
                                       untreat_var = "control",
                                       time_var = "year", 
                                       lag_control = "lag_control",
                                       compare_var = "state_year_commit", 
                                       unit_var= "state_program", d_var="log_grant", 
                                       lead_yrs = 2,lag_yrs = 2, df = committee_grants)
# Calculate change in grant amount (long difference)
commit_analysis_data <- commit_analysis_data %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)
# Bootstrap
commit_coefs <- matrix(NA, nrow=1000, ncol=3)
commit_analysis_data <- commit_analysis_data[order(commit_analysis_data$state_program, 
                                                   commit_analysis_data$year), ]
all_state_programs <- unique(commit_analysis_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- commit_analysis_data[commit_analysis_data$state_program%in%boot_state_program,]
  commit_coefs[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  commit_coefs[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  commit_coefs[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}
commit_analysis_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(commit_coefs, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(commit_coefs,2, quantile, 0.975),
            ci025=apply(commit_coefs,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),df=nrow(commit_analysis_data[commit_analysis_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(commit_analysis_data[commit_analysis_data$obs_weight!=0,])

### Regular Order Only
committee_grants$unorthodox <- ifelse(committee_grants$s_nocommreport +
                                        committee_grants$no_conference>0,1,0)
committee_grants$treated <- ifelse(committee_grants$chair==1 &
                                     committee_grants$unorthodox==0,1,0)

## Create lags and leads and calculate weights  
orth_commit_analysis_data <- make_boot_data(treat_var = "treated",
                                            untreat_var = "control",
                                            time_var = "year", 
                                            lag_control = "control",
                                            compare_var = "state_year_commit", 
                                            unit_var= "state_program", d_var="log_grant", 
                                            lead_yrs = 2,lag_yrs = 2, 
                                            df = committee_grants)

# Calculate change in grant amount (long difference)
orth_commit_analysis_data <- orth_commit_analysis_data %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)


# Bootstrap
commit_coefs3 <- matrix(NA, nrow=1000, ncol=3)
orth_commit_analysis_data <- orth_commit_analysis_data[order(orth_commit_analysis_data$state_program, 
                                                             orth_commit_analysis_data$year), ]
all_state_programs <- unique(orth_commit_analysis_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- orth_commit_analysis_data[orth_commit_analysis_data$state_program%in%boot_state_program,]
  commit_coefs3[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  commit_coefs3[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  commit_coefs3[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}

orth_commit_analysis_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(commit_coefs3, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(commit_coefs3, 2, quantile, 0.975),
            ci025=apply(commit_coefs3, 2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),df=nrow(orth_commit_analysis_data[orth_commit_analysis_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(orth_commit_analysis_data[orth_commit_analysis_data$obs_weight!=0,])

### Unorthodox Only
committee_grants$treated <- ifelse(committee_grants$chair==1 &
                                     committee_grants$unorthodox==1,1,0)
# Create lags and leads and calculate weights  
unorth_commit_analysis_data <- make_boot_data(treat_var = "treated",
                                              untreat_var = "control",
                                              time_var = "year", 
                                              lag_control = "control",
                                              compare_var = "state_year_commit", 
                                              unit_var= "state_program", d_var="log_grant", 
                                              lead_yrs = 2,lag_yrs = 2, df = committee_grants)

# Calculate change in grant amount (long difference)
unorth_commit_analysis_data <- unorth_commit_analysis_data %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)


# Bootstrap
commit_coefs2 <- matrix(NA, nrow=1000, ncol=3)
unorth_commit_analysis_data <- unorth_commit_analysis_data[order(unorth_commit_analysis_data$state_program, 
                                                                 unorth_commit_analysis_data$year), ]
all_state_programs <- unique(unorth_commit_analysis_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- unorth_commit_analysis_data[unorth_commit_analysis_data$state_program%in%boot_state_program,]
  commit_coefs2[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  commit_coefs2[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  commit_coefs2[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}

unorth_commit_analysis_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(commit_coefs2, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(commit_coefs2,2, quantile, 0.975),
            ci025=apply(commit_coefs2,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),df=nrow(unorth_commit_analysis_data[unorth_commit_analysis_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(unorth_commit_analysis_data[unorth_commit_analysis_data$obs_weight!=0,])

#### Chair Advantage Unorthodox vs Regular Order (Table A8) ----

grants <- read_csv("grants.csv")

## Create variables needed for analysis
grants <- grants %>%
  mutate(lag_control = 1 - chair, #units are only included if they switch to chair
         state_program = paste(state_abbr, program),
         year_cong = paste(year, congress),
         log_grant = log(grant+1),
         prior_year = year - 1) %>%
  left_join(select(grants,program,state,prior_year=year,prior_grant=grant)) %>%
  unique

## Impact of Conference
# Treated Units = became chair and no conference
# Control Units = became chair and had a conference
grants <- grants %>%
  mutate(unorthodox = ifelse(no_conference == 1, 1, 0),
         treated = chair * unorthodox, #treated units are unorthodox law and chair
         control = chair * (1 - unorthodox)) #control units are regular law and chair
# Create lags and leads and calculate weights  
conference_data <- make_boot_data(treat_var = "treated",untreat_var = "control",
                                  lag_control = "lag_control", time_var = "year", 
                                  compare_var = "year_cong", 
                                  unit_var= "state_program", d_var="log_grant", 
                                  lead_yrs = 2,lag_yrs = 2, df = grants)

# Calculate change in grant amount (long difference)
conference_data <- conference_data %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)

# Bootstrap
conference_coefs <- matrix(NA, nrow=1000, ncol=3)
conference_data <- conference_data[order(conference_data$state_program, 
                                         conference_data$year), ]
all_state_programs <- unique(conference_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- conference_data[conference_data$state_program%in%boot_state_program,]
  conference_coefs[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  conference_coefs[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  conference_coefs[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}

conference_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(conference_coefs, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(conference_coefs,2, quantile, 0.975),
            ci025=apply(conference_coefs,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),
                     df=nrow(conference_data[conference_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(conference_data[conference_data$obs_weight!=0,])

## Impact of No Senate Committee Report
# Treated Units = became chair and no report
# Control Units = became chair and reported

grants <- grants %>%
  mutate(unorthodox = ifelse(s_nocommreport == 1, 1, 0),
         treated = chair * unorthodox, #treated units are unorthodox law and chair
         control = chair * (1 - unorthodox)) #control units are regular law and chair

## Create lags and leads and calculate weights  
report_data <- make_boot_data(treat_var = "treated",untreat_var = "control",
                              lag_control = "lag_control", time_var = "year", 
                              compare_var = "year_cong", 
                              unit_var= "state_program", d_var="log_grant", 
                              lead_yrs = 2,lag_yrs = 2, df = grants)

# Calculate change in grant amount (long difference)
report_data <- report_data %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)

# Bootstrap
report_coefs <- matrix(NA, nrow=1000, ncol=3)
report_data <- report_data[order(report_data$state_program, 
                                 report_data$year), ]
all_state_programs <- unique(report_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- report_data[report_data$state_program%in%boot_state_program,]
  report_coefs[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  report_coefs[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  report_coefs[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}

report_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(report_coefs, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(report_coefs,2, quantile, 0.975),
            ci025=apply(report_coefs,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),
                     df=nrow(report_data[report_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(report_data[report_data$obs_weight!=0,])

### Composite Unorthodox
# Treated Units = became chair and unorthodox reauth
# Control Units = became chair and regular reauth
# Unorthodox = didn't have committee report and/or conference

grants <- grants %>%
  mutate(unorthodox = ifelse(s_nocommreport + no_conference > 0, 1, 0),
         treated = chair * unorthodox, #treated units are unorthodox law and chair
         control = chair * (1 - unorthodox)) #control units are regular law and chair

## Create lags and leads and calculate weights  
unorthodox_data <- make_boot_data(treat_var = "treated",untreat_var = "control",
                                  lag_control = "lag_control", time_var = "year", 
                                  compare_var = "year_cong", 
                                  unit_var= "state_program", d_var="log_grant", 
                                  lead_yrs = 2,lag_yrs = 2, df = grants)

# Calculate change in grant amount (long difference)
unorthodox_data <- unorthodox_data %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)

# Bootstrap
unorthodox_coefs <- matrix(NA, nrow=1000, ncol=3)
unorthodox_data <- unorthodox_data[order(unorthodox_data$state_program, 
                                         unorthodox_data$year), ]
all_state_programs <- unique(unorthodox_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- unorthodox_data[unorthodox_data$state_program%in%boot_state_program,]
  unorthodox_coefs[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  unorthodox_coefs[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  unorthodox_coefs[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}

unorthodox_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(unorthodox_coefs, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(unorthodox_coefs,2, quantile, 0.975),
            ci025=apply(unorthodox_coefs,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),
                     df=nrow(unorthodox_data[unorthodox_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(unorthodox_data[unorthodox_data$obs_weight!=0,])







#### Majority Party Committee Advantage (Table A9) ----
# Treated Units = join committee and reauthorized
# Control Units = join committee and have yet to reauthorize
grants <- read_csv("grants.csv")

## All Legislation
# Create new treated and control variables
committee_grants <- grants %>%
  filter(!is.na(on_commit) & !is.na(on_commit_prior) & !is.na(grant)) %>%
  mutate(treated = committee_maj,
         control = 1 - on_commit, # look at switching on to committee
         state_year_commit = paste0(state_abbr,year,committee_code),
         state_program = paste0(state_abbr, program),
         commit_year = paste(committee_code, year),
         log_grant = log(grant+1)) 
## Create lags and leads and calculate weights  
commit_analysis_data <- make_boot_data(treat_var = "treated",
                                       untreat_var = "control",
                                       time_var = "year", 
                                       lag_control = "control",
                                       compare_var = "state_year_commit", 
                                       unit_var= "state_program", d_var="log_grant", 
                                       lead_yrs = 2,lag_yrs = 2, df = committee_grants)

# Calculate change in grant amount (long difference)
commit_analysis_data <- commit_analysis_data %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)


# Bootstrap
commit_coefs <- matrix(NA, nrow=1000, ncol=3)
commit_analysis_data <- commit_analysis_data[order(commit_analysis_data$state_program, 
                                                   commit_analysis_data$year), ]
all_state_programs <- unique(commit_analysis_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- commit_analysis_data[commit_analysis_data$state_program%in%boot_state_program,]
  commit_coefs[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  commit_coefs[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  commit_coefs[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}

commit_analysis_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(commit_coefs, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(commit_coefs,2, quantile, 0.975),
            ci025=apply(commit_coefs,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),df=nrow(commit_analysis_data[commit_analysis_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(commit_analysis_data[commit_analysis_data$obs_weight!=0,])

### Regular Order
committee_grants$unorthodox <- ifelse(committee_grants$s_nocommreport + 
                                        committee_grants$no_conference > 0, 1, 0)
committee_grants$treated <- ifelse(committee_grants$committee_maj==1 & 
                                     committee_grants$unorthodox==0, 1, 0)

## Create lags and leads and calculate weights  
orth_commit_analysis_data <- make_boot_data(treat_var = "treated",
                                            untreat_var = "control",
                                            time_var = "year", 
                                            lag_control = "control",
                                            compare_var = "state_year_commit", 
                                            unit_var= "state_program", d_var="log_grant", 
                                            lead_yrs = 2,lag_yrs = 2, 
                                            df = committee_grants)

# Calculate change in grant amount (long difference)
orth_commit_analysis_data <- orth_commit_analysis_data %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)


# Bootstrap
commit_coefs3 <- matrix(NA, nrow=1000, ncol=3)
orth_commit_analysis_data <- orth_commit_analysis_data[order(orth_commit_analysis_data$state_program, 
                                                             orth_commit_analysis_data$year), ]
all_state_programs <- unique(orth_commit_analysis_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- orth_commit_analysis_data[orth_commit_analysis_data$state_program%in%boot_state_program,]
  commit_coefs3[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  commit_coefs3[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  commit_coefs3[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}

orth_commit_analysis_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(commit_coefs3, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(commit_coefs3, 2, quantile, 0.975),
            ci025=apply(commit_coefs3, 2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),df=nrow(orth_commit_analysis_data[orth_commit_analysis_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(orth_commit_analysis_data[orth_commit_analysis_data$obs_weight!=0,])

##### Unorthodox Bills Only
committee_grants$treated <- committee_grants$committee_maj * committee_grants$unorthodox

## Create lags and leads and calculate weights  
unorth_commit_analysis_data <- make_boot_data(treat_var = "treated",
                                              untreat_var = "control",
                                              time_var = "year", 
                                              lag_control = "control",
                                              compare_var = "state_year_commit", 
                                              unit_var= "state_program", d_var="log_grant", 
                                              lead_yrs = 2,lag_yrs = 2, df = committee_grants)

# Calculate change in grant amount (long difference)
unorth_commit_analysis_data <- unorth_commit_analysis_data %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)


# Bootstrap
commit_coefs2 <- matrix(NA, nrow=1000, ncol=3)
unorth_commit_analysis_data <- unorth_commit_analysis_data[order(unorth_commit_analysis_data$state_program, 
                                                                 unorth_commit_analysis_data$year), ]
all_state_programs <- unique(unorth_commit_analysis_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- unorth_commit_analysis_data[unorth_commit_analysis_data$state_program%in%boot_state_program,]
  commit_coefs2[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  commit_coefs2[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  commit_coefs2[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}

unorth_commit_analysis_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(commit_coefs2, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(commit_coefs2,2, quantile, 0.975),
            ci025=apply(commit_coefs2,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),df=nrow(unorth_commit_analysis_data[unorth_commit_analysis_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(unorth_commit_analysis_data[unorth_commit_analysis_data$obs_weight!=0,])



#### Majority Party Unorthodox vs. Regular Order (Table A10) ----
# Read in grants data
grants <- read_csv("grants.csv")
# Create variables needed for analysis
grants <- grants %>%
  mutate(lag_control = 1 - on_commit, #units are only included if they switch onto committee
         state_program = paste(state_abbr, program),
         year_cong = paste(year, congress),
         log_grant = log(grant+1),
         prior_year = year - 1) %>%
  left_join(select(grants,program,state,prior_year=year,prior_grant=grant)) %>%
  unique

### Impact of Conference 
# Treated Units = maj committee and no conference
# Control Units = maj committee and had a conference

grants <- grants %>%
  mutate(unorthodox = ifelse(no_conference == 1, 1, 0),
         treated = committee_maj * unorthodox, #treated units are unorthodox law and maj committee
         control = committee_maj * (1 - unorthodox)) #control units are regular law and maj committee

## Create lags and leads and calculate weights  
conference_data <- make_boot_data(treat_var = "treated",untreat_var = "control",
                                  lag_control = "lag_control", time_var = "year", 
                                  compare_var = "year_cong", 
                                  unit_var= "state_program", d_var="log_grant", 
                                  lead_yrs = 2,lag_yrs = 2, df = grants)

# Calculate change in grant amount (long difference)
conference_data <- conference_data %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)

# Bootstrap
conference_coefs <- matrix(NA, nrow=1000, ncol=3)
conference_data <- conference_data[order(conference_data$state_program, 
                                         conference_data$year), ]
all_state_programs <- unique(conference_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- conference_data[conference_data$state_program%in%boot_state_program,]
  conference_coefs[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  conference_coefs[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  conference_coefs[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}

conference_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(conference_coefs, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(conference_coefs,2, quantile, 0.975),
            ci025=apply(conference_coefs,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),
                     df=nrow(conference_data[conference_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(conference_data[conference_data$obs_weight!=0,])

## Impact of No Senate Committee Report
grants <- grants %>%
  mutate(unorthodox = ifelse(s_nocommreport == 1, 1, 0),
         treated = committee_maj * unorthodox, #treated units are unorthdox law and on committee
         control = committee_maj * (1 - unorthodox)) #control units are regular law and on committee 

## Create lags and leads and calculate weights  
report_data <- make_boot_data(treat_var = "treated",untreat_var = "control",
                              lag_control = "lag_control", time_var = "year", 
                              compare_var = "year_cong", 
                              unit_var= "state_program", d_var="log_grant", 
                              lead_yrs = 2,lag_yrs = 2, df = grants)

# Calculate change in grant amount (long difference)
report_data <- report_data %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)

# Bootstrap
report_coefs <- matrix(NA, nrow=1000, ncol=3)
report_data <- report_data[order(report_data$state_program, 
                                 report_data$year), ]
all_state_programs <- unique(report_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- report_data[report_data$state_program%in%boot_state_program,]
  report_coefs[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  report_coefs[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  report_coefs[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}

report_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(report_coefs, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(report_coefs,2, quantile, 0.975),
            ci025=apply(report_coefs,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),
                     df=nrow(report_data[report_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(report_data[report_data$obs_weight!=0,])


## Composite Unorthodox
grants <- grants %>%
  mutate(unorthodox = ifelse(s_nocommreport + no_conference > 0, 1, 0),
         treated = committee_maj * unorthodox, #treated units are unorthodox law and on committee
         control = committee_maj * (1 - unorthodox)) #control units are regular law and on committee 

## Create lags and leads and calculate weights  
unorthodox_data <- make_boot_data(treat_var = "treated",untreat_var = "control",
                                  lag_control = "lag_control", time_var = "year", 
                                  compare_var = "year_cong", 
                                  unit_var= "state_program", d_var="log_grant", 
                                  lead_yrs = 2,lag_yrs = 2, df = grants)

# Calculate change in grant amount (long difference)
unorthodox_data <- unorthodox_data %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)

# Bootstrap
unorthodox_coefs <- matrix(NA, nrow=1000, ncol=3)
unorthodox_data <- unorthodox_data[order(unorthodox_data$state_program, 
                                         unorthodox_data$year), ]
all_state_programs <- unique(unorthodox_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- unorthodox_data[unorthodox_data$state_program%in%boot_state_program,]
  unorthodox_coefs[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  unorthodox_coefs[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  unorthodox_coefs[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}

unorthodox_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(unorthodox_coefs, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(unorthodox_coefs,2, quantile, 0.975),
            ci025=apply(unorthodox_coefs,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),
                     df=nrow(unorthodox_data[unorthodox_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(unorthodox_data[unorthodox_data$obs_weight!=0,])







#### Minority Party Committee Advantage (Table A11) ----
# Treated Units = joined committee and reauthorized
# Control Units = joined committee and have yet to reauthorize
# Match based on state & committee
# reason to match this way is then you only look at states represented by
# committee members to mirror the previous analysis

# Read in the data again
grants <- read_csv("grants.csv")

### All Legislation
# Create new treated and control variables
committee_grants <- grants %>%
  filter(!is.na(on_commit) & !is.na(on_commit_prior) & !is.na(grant)) %>%
  mutate(treated = committee_min,
         control = 1 - on_commit, # look at switching on to committee
         state_year_commit = paste0(state_abbr,year,committee_code),
         state_program = paste0(state_abbr, program),
         commit_year = paste(committee_code, year),
         log_grant = log(grant+1))
## Create lags and leads and calculate weights  
commit_analysis_data <- make_boot_data(treat_var = "treated",
                                       untreat_var = "control",
                                       time_var = "year", 
                                       lag_control = "control",
                                       compare_var = "state_year_commit", 
                                       unit_var= "state_program", d_var="log_grant", 
                                       lead_yrs = 2,lag_yrs = 2, df = committee_grants)
# Calculate change in grant amount (long difference)
commit_analysis_data <- commit_analysis_data %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)

# Bootstrap
commit_coefs <- matrix(NA, nrow=1000, ncol=3)
commit_analysis_data <- commit_analysis_data[order(commit_analysis_data$state_program, 
                                                   commit_analysis_data$year), ]
all_state_programs <- unique(commit_analysis_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- commit_analysis_data[commit_analysis_data$state_program%in%boot_state_program,]
  commit_coefs[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  commit_coefs[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  commit_coefs[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}
commit_analysis_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(commit_coefs, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(commit_coefs,2, quantile, 0.975),
            ci025=apply(commit_coefs,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),df=nrow(commit_analysis_data[commit_analysis_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(commit_analysis_data[commit_analysis_data$obs_weight!=0,])

### Regular Order
committee_grants$unorthodox <- ifelse(committee_grants$s_nocommreport + 
                                        committee_grants$no_conference > 0,1,0)
committee_grants$treated <- ifelse(committee_grants$committee_min==1 & 
                                     committee_grants$unorthodox==0, 1, 0)
## Create lags and leads and calculate weights  
orth_commit_analysis_data <- make_boot_data(treat_var = "treated",
                                            untreat_var = "control",
                                            time_var = "year", 
                                            lag_control = "control",
                                            compare_var = "state_year_commit", 
                                            unit_var= "state_program", d_var="log_grant", 
                                            lead_yrs = 2,lag_yrs = 2, 
                                            df = committee_grants)

# Calculate change in grant amount (long difference)
orth_commit_analysis_data <- orth_commit_analysis_data %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)


# Bootstrap
commit_coefs3 <- matrix(NA, nrow=1000, ncol=3)
orth_commit_analysis_data <- orth_commit_analysis_data[order(orth_commit_analysis_data$state_program, 
                                                             orth_commit_analysis_data$year), ]
all_state_programs <- unique(orth_commit_analysis_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- orth_commit_analysis_data[orth_commit_analysis_data$state_program%in%boot_state_program,]
  commit_coefs3[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  commit_coefs3[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  commit_coefs3[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}

orth_commit_analysis_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(commit_coefs3, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(commit_coefs3, 2, quantile, 0.975),
            ci025=apply(commit_coefs3, 2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),df=nrow(orth_commit_analysis_data[orth_commit_analysis_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(orth_commit_analysis_data[orth_commit_analysis_data$obs_weight!=0,])

### Unorthodox Legislation Only
# Create new treated variable
committee_grants$treated <- committee_grants$committee_min * 
  committee_grants$unorthodox
## Create lags and leads and calculate weights  
unorth_commit_analysis_data <- make_boot_data(treat_var = "treated",
                                              untreat_var = "control",
                                              time_var = "year", 
                                              lag_control = "control",
                                              compare_var = "state_year_commit", 
                                              unit_var= "state_program", d_var="log_grant", 
                                              lead_yrs = 2,lag_yrs = 2, df = committee_grants)

# Calculate change in grant amount (long difference)
unorth_commit_analysis_data <- unorth_commit_analysis_data %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)


# Bootstrap
commit_coefs2 <- matrix(NA, nrow=1000, ncol=3)
unorth_commit_analysis_data <- unorth_commit_analysis_data[order(unorth_commit_analysis_data$state_program, 
                                                                 unorth_commit_analysis_data$year), ]
all_state_programs <- unique(unorth_commit_analysis_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- unorth_commit_analysis_data[unorth_commit_analysis_data$state_program%in%boot_state_program,]
  commit_coefs2[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  commit_coefs2[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  commit_coefs2[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}

unorth_commit_analysis_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(commit_coefs2, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(commit_coefs2,2, quantile, 0.975),
            ci025=apply(commit_coefs2,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),df=nrow(unorth_commit_analysis_data[unorth_commit_analysis_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(unorth_commit_analysis_data[unorth_commit_analysis_data$obs_weight!=0,])



#### Minority Party Unorthodox vs Regular Order (Table A12) ----
# Read in grants data
grants <- read_csv("grants.csv")
# Create variables needed for analysis
grants <- grants %>%
  mutate(lag_control = 1 - on_commit, #units are only included if they switch onto committee
         state_program = paste(state_abbr, program),
         year_cong = paste(year, congress),
         log_grant = log(grant+1),
         prior_year = year - 1) %>%
  left_join(select(grants,program,state,prior_year=year,prior_grant=grant)) %>%
  unique

## Impact of Conference
# Treated Units = min committee and no conference
# Control Units = min committee and had a conference
grants <- grants %>%
  mutate(unorthodox = ifelse(no_conference == 1, 1, 0),
         treated = committee_min * unorthodox, #treated units are unorthodox law and maj committee
         control = committee_min * (1 - unorthodox)) #control units are regular law and maj committee

## Create lags and leads and calculate weights  
conference_data <- make_boot_data(treat_var = "treated",untreat_var = "control",
                                  lag_control = "lag_control", time_var = "year", 
                                  compare_var = "year_cong", 
                                  unit_var= "state_program", d_var="log_grant", 
                                  lead_yrs = 2,lag_yrs = 2, df = grants)
# Calculate change in grant amount (long difference)
conference_data <- conference_data %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)
# Bootstrap
conference_coefs <- matrix(NA, nrow=1000, ncol=3)
conference_data <- conference_data[order(conference_data$state_program, 
                                         conference_data$year), ]
all_state_programs <- unique(conference_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- conference_data[conference_data$state_program%in%boot_state_program,]
  conference_coefs[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  conference_coefs[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  conference_coefs[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}
conference_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(conference_coefs, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(conference_coefs,2, quantile, 0.975),
            ci025=apply(conference_coefs,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),
                     df=nrow(conference_data[conference_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(conference_data[conference_data$obs_weight!=0,])


### Impact of No Senate Committee Report
# Treated Units = switched onto committee and no report
# Control Units = switched onto committee and was reported
grants <- grants %>%
  mutate(unorthodox = ifelse(s_nocommreport == 1, 1, 0),
         treated = committee_min * unorthodox, #treated units are unorthodox law and on committee
         control = committee_min * (1 - unorthodox)) #control units are regular law and on committee 
## Create lags and leads and calculate weights  
report_data <- make_boot_data(treat_var = "treated",untreat_var = "control",
                              lag_control = "lag_control", time_var = "year", 
                              compare_var = "year_cong", 
                              unit_var= "state_program", d_var="log_grant", 
                              lead_yrs = 2,lag_yrs = 2, df = grants)
# Calculate change in grant amount (long difference)
report_data <- report_data %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)
# Bootstrap
report_coefs <- matrix(NA, nrow=1000, ncol=3)
report_data <- report_data[order(report_data$state_program, 
                                 report_data$year), ]
all_state_programs <- unique(report_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- report_data[report_data$state_program%in%boot_state_program,]
  report_coefs[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  report_coefs[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  report_coefs[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}
report_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(report_coefs, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(report_coefs,2, quantile, 0.975),
            ci025=apply(report_coefs,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),
                     df=nrow(report_data[report_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(report_data[report_data$obs_weight!=0,])

### Composite Unorthodox
# Treated Units = switched onto committee and unorthodox reauth
# Control Units = switched onto committee and regular reauth
# Unorthodox = didn't have committee report and/or conference
grants <- grants %>%
  mutate(unorthodox = ifelse(s_nocommreport + no_conference > 0, 1, 0),
         treated = committee_min * unorthodox, #treated units are unorthdox law and on committee
         control = committee_min * (1 - unorthodox)) #control units are regular law and on committee 

## Create lags and leads and calculate weights  
unorthodox_data <- make_boot_data(treat_var = "treated",untreat_var = "control",
                                  lag_control = "lag_control", time_var = "year", 
                                  compare_var = "year_cong", 
                                  unit_var= "state_program", d_var="log_grant", 
                                  lead_yrs = 2,lag_yrs = 2, df = grants)

# Calculate change in grant amount (long difference)
unorthodox_data <- unorthodox_data %>%
  mutate(grant_change = log_grant - log_grant_p1,
         grant_change_t1 = log_grant_t1 - log_grant_p1,
         grant_change_t2 = log_grant_t2 - log_grant_p1)

# Bootstrap
unorthodox_coefs <- matrix(NA, nrow=1000, ncol=3)
unorthodox_data <- unorthodox_data[order(unorthodox_data$state_program, 
                                         unorthodox_data$year), ]
all_state_programs <- unique(unorthodox_data$state_program) #this is what we sample from
set.seed(1965)
for (i in 1:1000){
  boot_state_program <- sample(all_state_programs, size=length(all_state_programs),
                               replace=T)
  data_sub <- unorthodox_data[unorthodox_data$state_program%in%boot_state_program,]
  unorthodox_coefs[i,1] <- sum(data_sub$grant_change * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  unorthodox_coefs[i,2] <- sum(data_sub$grant_change_t1 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
  unorthodox_coefs[i,3] <- sum(data_sub$grant_change_t2 * data_sub$obs_weight, na.rm=T) / 
    sum(data_sub$treated * data_sub$obs_weight, na.rm=T)
}

unorthodox_data %>%
  select(starts_with("grant_change"),obs_weight,treated) %>%
  rename(grant_change_t = grant_change) %>%
  pivot_longer(cols = c(starts_with("grant_change")),
               names_to = "time", names_pattern="_(t.*)",
               values_to = "grant_change") %>%
  group_by(time) %>%
  summarise(estimate=sum(grant_change*obs_weight,na.rm=T)/
              sum(treated*obs_weight,na.rm=T)) %>%
  bind_cols(std_error=apply(unorthodox_coefs, 2, function(x) sqrt(sum((x - mean(x))^2)/999)),
            ci975=apply(unorthodox_coefs,2, quantile, 0.975),
            ci025=apply(unorthodox_coefs,2, quantile, 0.025)) %>%
  # for p-value calculation, use conservative estimate of degrees of freedom
  # rather than all observations use only non-zero weighted ones
  mutate(tstat=estimate/std_error,
         pvalue=2*pt(-abs(tstat),
                     df=nrow(unorthodox_data[unorthodox_data$obs_weight!=0,])-1)) %>%
  print_estimates()
# Number of observations with  non-zero weight
nrow(unorthodox_data[unorthodox_data$obs_weight!=0,])



